<?php
/**
 * Sites Model
 *
 * PHP Version 5
 *
 * @package    Ecga
 * @subpackage Models
 * @author     FarmerLi <lixu_01@126.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN:$Id: projects.php 3051 2011-01-11 13:06:18Z sunbo $
 */

namespace Project\Apps\Ecga\Models;

use \Project\Models\Abstraction\Ecga\Sites as bizSites;
use \Project\Apps\Ecga\Library\Visitor;
/**
 * Sites Model
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    FarmerLi <lixu_01@126.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: 0.1
 */
class Sites extends bizSites
{
    /**
     * 获取站点列表
     *
     * @author Garbin <huangjiabin@ec3s.com>
     * 
     * @param int $projectId 项目ID
     * 
     * @return void
     */
    public function getSites($projectId)
    {
        static $return = null;
        
        if (null === $return) {
            $select = $this->select(
                'id',
                'ename',
                'cname',
                'siteUrl',
                'region',
                'tag'
            );
            if ($projectId != '') {
                $return = $select->where('projectId', '=', $projectId)
                        ->orderBy('region', 'ASC')->fetch();
            } else {
                $return = $select->orderBy('region', 'ASC')->fetch();
            }
            $return = iterator_to_array($return);
        }
        return $return;
    }
    /**
     * 根据项目获取采集站点列表
     *
     * @param int $projectId 项目ID
     *
     * @author Wanggeng <wanggeng@ec3s.com>
     * @return void 
     */
    public function getSitesByProject($projectId)
    {
        $return = null;  
        
        if (null === $return) {
            $select = $this->select(
                'id',
                'ename',
                'cname',
                'siteUrl'
            )->where('projectId', '=', $projectId);
            $return = $select->fetch();
            $return = iterator_to_array($return);
        }
        return $return;
    } 
    /**
     * 获取所有
     * 
     * @return object
     */
    public function getAll()
    {
        $this->select()
            ->fetch();
    }
    
    /**
     * 获取渠道所有Tag 线上授权OR线下授权
     * 
     * @return object
     */
    public function getTag()
    {
        $result = $this->select('tag')
            ->groupBy('tag')
            ->fetch();
        return $result;
    }
    
    /**
     * 取分页query
     *
     * @param array $projectIdArray 项目ID
     * @param bool  $isBoss         是否是Boss帐号
     *
     * @return query $query
     *
     * @author sunfengming
     */
    public static function getPageQuery($projectIdArray, $isBoss)
    {  
        $model = model('Sites');
        if ($isBoss) {
            return $model->query($model->filter())->fields('id', 'ename', 'cname', 'siteUrl', 'projectId', 'tag');
        } else {
            return $model->query($model->filter())->fields('id', 'ename', 'cname', 'siteUrl', 'projectId', 'tag')
                ->where('projectId', 'IN', $projectIdArray);
        }
    }
}